package download

import (
	"errors"
	"log"
	"net/http"
	"poetry/database"
	"poetry/model"
	"strconv"

	"github.com/PuerkitoBio/goquery"
)

func requestURL(url string) (*goquery.Document, error) {
	request, err := http.Get(url)
	if err != nil {
		return nil, errors.New("download html failed")
	}

	request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
	defer request.Body.Close()
	return goquery.NewDocumentFromReader(request.Body)
}

// PushDataIntoDB 爬虫
func PushDataIntoDB() {
	log.Println("------------------start------------------")
	for i := 1; i < 100; i++ {
		index := strconv.Itoa(i)
		url := "https://so.gushiwen.org/authors/default.aspx?p=" + index + "&c="
		document, _ := requestURL(url)
		document.Find(".main3 > div.left > .sonspic").Each(func(i int, selection *goquery.Selection) {
			name := selection.Find(".cont b").Text()
			profile := selection.Find(".cont p:last-child").Text()
			var avatar string
			img, ok := selection.Find(".cont .divimg a img").Attr("src")
			if ok == true {
				avatar = img
			} else {
				avatar = ""
			}
			works := selection.Find(".cont p:last-child a").Text()
			log.Println(name)
			log.Println(profile)
			log.Println(works)
			log.Println(avatar)

			author := model.Author{
				Name:    name,
				Profile: profile,
				Works:   works,
				Avatar:  avatar,
			}
			database.DB.Save(&author)
		})
		log.Println("-------------------------------------", i)
	}
	log.Println("------------------end------------------")
}

func GetWorks() {
	var authors []model.Author
	var count int
	database.DB.Model(&model.Author{}).Count(&count).Find(&authors)
	for i := 0; i < count - 1; i++ {
		for j := 1; j < 10; i++ {
			url := "https://so.gushiwen.cn/search.aspx?type=author&page=" + strconv.Itoa(j) + "&value=" + authors[i].Name
			document, _ := requestURL(url)
			document.Find("body > div.main3 > div.left > div.sons").Each(func(i int, selection *goquery.Selection) {
				// 诗名
				title := selection.Find("div.cont > p:nth-child(2) > a > b").Text()
				// 朝代
				dynasty := selection.Find("div.cont > p.source > a:nth-child(1)").Text()
				// 作者
				name := selection.Find("div.cont > p.source > a:nth-child(3)").Text()
				// 诗词 document.querySelector("#contsond59ec5d6c91c")
				content := selection.Find("div.cont > .contson").Text()
				// 标签 document.querySelector("body > div.main3 > div.left > div:nth-child(5) > div.tag")
				tags := selection.Find("div.tag").Text()
	
				poetry := model.Poetry{
					Title:   title,
					Dynasty: dynasty,
					Name:    name,
					Content: content,
					Tags:    tags,
				}
				log.Println(poetry)
				database.DB.Save(&poetry)
			})
		}
	}
}
